/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <baja/transition_tracking/transition_tracking_interfaces.h>
#include <baja/transition_tracking//transition_tracking_internal_interfaces.h>
#include <stack>

namespace baja { namespace transition_tracking {

class transitions_bundle :
    public iinternal_transitions_bundle,
    public std::enable_shared_from_this<transitions_bundle>
{
    struct transition
    {
        std::vector<transition_path_node> absolutePath;
        std::shared_ptr<itransition_parameters> transitionParameters;
    };

public:
    transitions_bundle();
    static std::shared_ptr<itransitions_bundle> create();

    // itransition_bundle
    void addTransition(
        const std::vector<transition_path_node>& absolutePath,
        const std::shared_ptr<itransition_parameters>& transitionParameters
        );

    // iinternal_transitions_bundle
    void undo(
        const std::shared_ptr<itransition_target>& rootTarget
        );

    void redo(
        const std::shared_ptr<itransition_target>& rootTarget
        );

private:
    std::stack<std::shared_ptr<transition>> _undoQueue;
    std::stack<std::shared_ptr<transition>> _redoQueue;
};

class transition_tracking_service :
    public itransition_tracking_service,
    public std::enable_shared_from_this<transition_tracking_service>
{
    class undo_redo_context
    {
        bool* _undoRedoInProgress;
    public:
        undo_redo_context(bool* undoRedoInProgress)
        {
            _undoRedoInProgress = undoRedoInProgress;
            *_undoRedoInProgress = true;
        }
        ~undo_redo_context()
        {
            *_undoRedoInProgress = false;
        }
    };

public:
    // constructors
    transition_tracking_service();
    static std::shared_ptr<transition_tracking_service> create();

    // itransition_tracking_service
    void initialize();

    void registerRootTransitionTarget(
        std::shared_ptr<itransition_target> rootTarget
        );

    baja::uint32 generateTargetTypeToken();

    void addTransitionsBundle(
        const std::shared_ptr<itransitions_bundle>& transitionBundle
        );

    void startTransitionBundle();
    void endTransitionBundle();

    void addTransition(
        const std::vector<transition_path_node>& absolutePath,
        const std::shared_ptr<itransition_parameters>& transitionParameters
        );

    void undo();
    void redo();

private:

    bool _undoRedoInProgress;
    baja::uint32 _nestingLevel;
    std::shared_ptr<itransitions_bundle> _currentBundle;

    std::stack<std::shared_ptr<iinternal_transitions_bundle>> _undoBundleQueue;
    std::stack<std::shared_ptr<iinternal_transitions_bundle>> _redoBundleQueue;

    baja::uint32 _typeTokenSeed;

    std::shared_ptr<itransition_target> _rootTarget;

    // methods
    void clearRedoQueue();
};

std::shared_ptr<itransition_tracking_service> createTransitionTrackingService();

}} // baja::transition_tracking